package com.yc.damai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.damai.entity.Orders;
import com.yc.damai.entity.Product;
import com.yc.damai.mapper.ProductMapper;
import com.yc.damai.vo.Result;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ly
 * @since 2022-11-01
 */
@RestController
@RequestMapping("/damai/product")
public class ProductController {

    @Resource
    ProductMapper productMapper;

    @RequestMapping("queryHots")
    List<Product> queryHots(){
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Product::getIsHot, 1);
        return productMapper.selectList(lqw);
    }

    // 一般情况下事务注解应该加在业务层
    @Transactional
    @RequestMapping("updateCount")
    Result updateCount(@RequestBody Orders orders){
        orders.getOrderitems().forEach(item->{
            productMapper.updateStore(item.getPid(), item.getCount());
        });
        return new Result(1, "库存修改成功", null);
    }

    @RequestMapping("queryAll")
    List<Product> queryAll(){
        return productMapper.selectList(null);
    }

}

